from selenium import webdriver
import time


driver = webdriver.Chrome()
driver.get("https://kns.cnki.net/kns8s/defaultresult/index?korder=&kw=")
time.sleep(3)

driver.find_element("xpath", "//div[@class='input-box']/input[@id='txt_search']").send_keys("爬虫")
time.sleep(3)
driver.find_element("xpath", "//div[@class='input-box']/input[@class='search-btn']").click()
time.sleep(3)
i = 0
while True:
    time.sleep(5)
    i += 1

    paper_list = driver.find_elements("xpath", "//table[@class='result-table-list']/tbody/tr")
    for paper in paper_list:
        data_list = []
        try:
            paper_name = paper.find_element("xpath", ".//td[@class='name']/a[@class='fz14']").text
        except:
            paper_name = "暂无标题"
        data_list.append(paper_name)
        try:
            paper_author = paper.find_element("xpath", ".//td[@class='author']").text
        except:
            paper_author = "暂无作者"
        data_list.append(paper_author)
        try:
            paper_url = paper.find_element("xpath", ".//td[@class='name']/a[@class='fz14']").get_attribute("href")
        except:
            paper_url = "暂无链接"
        data_list.append(paper_url)
        try:
            paper_source = paper.find_element("xpath", ".//td[@class='source']/p/a").text
        except:
            paper_source = "暂无来源"
        data_list.append(paper_source)
        try:
            paper_time = paper.find_element("xpath", ".//td[@class='date']").text
        except:
            paper_time = "暂无时间"
        data_list.append(paper_time)
        try:
            paper_download_num = paper.find_element("xpath",
                                                    ".//td[@class='download']/div/a[@class='downloadCnt']").text
        except:
            paper_download_num = "0"
        data_list.append(paper_download_num)
        print(paper_name, paper_author, paper_url, paper_source, paper_time, paper_download_num)
        time.sleep(1)
        data_list = ",".join(data_list)
        with open("./知网信息/数据.txt", "a", encoding="utf-8") as f:
            f.write(data_list+"\n")  # 写入文件
    driver.find_element("xpath", "//div[@class='pages']/a[@id='PageNext']").click()
    with open("./知网信息/数据.txt", "a", encoding="utf-8") as f:
        f.write(f"第{i}页"+"\n")  # 写入文件
